package org.example.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.example.bean.RoomBean;
import org.example.bean.StudentBean;
import org.example.mapper.IStudentMapper;
import org.example.service.IStudentService;

public class StudentServiceImp extends BaseService implements IStudentService{

    @Override
    public PageInfo<StudentBean> cutFind(Integer pageNum, String stuName, String stuClassName, String stuRoom) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        PageHelper.startPage(pageNum,3);
        PageInfo<StudentBean> pg = PageInfo.of(mapper.cutFind(stuName,stuClassName,stuRoom));
        session.close();
        return pg;
    }

    @Override
    public void add(StudentBean studentBean,Integer classID,Integer roomID) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.add(studentBean,classID,roomID);
        session.commit();
        session.close();
    }

    @Override
    public void changeRoom(String stuID, String newRoomID) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.changeRoom(stuID,newRoomID);
        session.commit();
        session.close();
    }

    @Override
    public void delStudent(Integer stuID) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        mapper.delStudent(stuID);
        session.commit();
        session.close();
    }

    @Override
    public StudentBean findByID(Integer stuID) {
        SqlSession session = this.getSession();
        IStudentMapper mapper = session.getMapper(IStudentMapper.class);
        StudentBean studentBean = mapper.findByID(stuID);
        session.close();
        return studentBean;
    }
}
